package com.yiman.project.system.service.impl;

import com.yiman.framework.shiro.service.PasswordService;
import com.yiman.project.system.mapper.UserMapper;
import com.yiman.project.system.mapper.UserRoleMapper;
import com.yiman.project.system.model.User;
import com.yiman.project.system.model.UserRoleKey;
import com.yiman.project.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: Mr.yang
 * @create: 2019-06-05
 **/
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    UserRoleMapper userRoleMapper;
    @Autowired
    PasswordService passwordService;
    @Override
    public List<User> selectUserList(User user) {
//        if(StringUtils.isNotEmpty(user.getUserName())){
//            user.setUserName("%"+user.getUserName()+"%");
//        }
        return userMapper.selectUserList(user);
    }

    @Override
    @Transactional
    public void insertUser(User user) {

        user.randomSalt();
        user.setPassword(passwordService.encryptPassword(user.getLoginName(), user.getPassword(), user.getSalt()));
        userMapper.insertSelective(user);
        // 新增用户与角色管理
        insertUserRole(user);
    }

    @Override
    @Transactional
    public void deleteUserByIds(String ids) {

        String[] arr = ids.split(",");

        final Integer[] userIds = new Integer[arr.length];
        for (int i = 0; i < arr.length; i++) {
            final Integer v = Integer.valueOf(arr[i]);
            userIds[i] = v;
        }

        userMapper.deleteUserByIds(userIds);
        // 删除用户与角色关联
        userRoleMapper.deleteUserRoleByIds(userIds);
    }

    @Override
    public User getUser(Integer userId) {
        return userMapper.selectByPrimaryKey(userId);
    }

    @Override
    @Transactional
    public void editSaveUser(User user) {
        userMapper.updateByPrimaryKeySelective(user);
        // 删除用户与角色关联
        userRoleMapper.deleteUserRoleByUserId(user.getUserId());
        // 新增用户与角色管理
        insertUserRole(user);
    }

    @Override
    public User selectUserByLoginName(String userName) {
        return userMapper.selectUserByLoginName(userName);
    }

    /**
     * 新增用户角色信息
     *
     * @param user 用户对象
     */
    public void insertUserRole(User user)
    {
        Integer[] roles = user.getRoleIds();
        if(roles != null){
            // 新增用户与角色管理
            List<UserRoleKey> list = new ArrayList<>();
            for (Integer roleId : roles) {
                if(roleId != null){
                    UserRoleKey ur = new UserRoleKey();
                    ur.setUserId(user.getUserId());
                    ur.setRoleId(roleId);
                    list.add(ur);
                }
            }
            if (list.size() > 0) {
                userRoleMapper.batchUserRole(list);
            }
        }
    }

}
